//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Common.DAL.Managers
{
    internal class CountryDBManager
    {
        #region Methods
        public static void DeleteCountry(int CountryID)
        {
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CountryDelete");
            db.AddInParameter(dbCommand, "CountryID", DbType.Int32, CountryID);
            int retValue = db.ExecuteNonQuery(dbCommand);
        }

        public static CountryCollection GetAllCountries(bool showHidden)
        {
            CountryCollection countryCollection = new CountryCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CountryLoadAll");
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    Country country = new Country();
                    country.CountryID = NopDataHelper.GetInt(dataReader, "CountryID");
                    country.Name = NopDataHelper.GetString(dataReader, "Name");
                    country.AllowsRegistration = NopDataHelper.GetBoolean(dataReader, "AllowsRegistration");
                    country.AllowsBilling = NopDataHelper.GetBoolean(dataReader, "AllowsBilling");
                    country.AllowsShipping = NopDataHelper.GetBoolean(dataReader, "AllowsShipping");
                    country.TwoLetterISOCode = NopDataHelper.GetString(dataReader, "TwoLetterISOCode");
                    country.ThreeLetterISOCode = NopDataHelper.GetString(dataReader, "ThreeLetterISOCode");
                    country.NumericISOCode = NopDataHelper.GetInt(dataReader, "NumericISOCode");
                    country.Published = NopDataHelper.GetBoolean(dataReader, "Published");
                    country.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    countryCollection.Add(country);
                }
            }
            return countryCollection;
        }

        public static CountryCollection GetAllCountriesForRegistration(bool showHidden)
        {
            CountryCollection countryCollection = new CountryCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CountryLoadAllForRegistration");
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    Country country = new Country();
                    country.CountryID = NopDataHelper.GetInt(dataReader, "CountryID");
                    country.Name = NopDataHelper.GetString(dataReader, "Name");
                    country.AllowsRegistration = NopDataHelper.GetBoolean(dataReader, "AllowsRegistration");
                    country.AllowsBilling = NopDataHelper.GetBoolean(dataReader, "AllowsBilling");
                    country.AllowsShipping = NopDataHelper.GetBoolean(dataReader, "AllowsShipping");
                    country.TwoLetterISOCode = NopDataHelper.GetString(dataReader, "TwoLetterISOCode");
                    country.ThreeLetterISOCode = NopDataHelper.GetString(dataReader, "ThreeLetterISOCode");
                    country.NumericISOCode = NopDataHelper.GetInt(dataReader, "NumericISOCode");
                    country.Published = NopDataHelper.GetBoolean(dataReader, "Published");
                    country.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    countryCollection.Add(country);
                }
            }
            return countryCollection;
        }

        public static CountryCollection GetAllCountriesForBilling(bool showHidden)
        {
            CountryCollection countryCollection = new CountryCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CountryLoadAllForBilling");
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    Country country = new Country();
                    country.CountryID = NopDataHelper.GetInt(dataReader, "CountryID");
                    country.Name = NopDataHelper.GetString(dataReader, "Name");
                    country.AllowsRegistration = NopDataHelper.GetBoolean(dataReader, "AllowsRegistration");
                    country.AllowsBilling = NopDataHelper.GetBoolean(dataReader, "AllowsBilling");
                    country.AllowsShipping = NopDataHelper.GetBoolean(dataReader, "AllowsShipping");
                    country.TwoLetterISOCode = NopDataHelper.GetString(dataReader, "TwoLetterISOCode");
                    country.ThreeLetterISOCode = NopDataHelper.GetString(dataReader, "ThreeLetterISOCode");
                    country.NumericISOCode = NopDataHelper.GetInt(dataReader, "NumericISOCode");
                    country.Published = NopDataHelper.GetBoolean(dataReader, "Published");
                    country.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    countryCollection.Add(country);
                }
            }
            return countryCollection;
        }

        public static CountryCollection GetAllCountriesForShipping(bool showHidden)
        {
            CountryCollection countryCollection = new CountryCollection();
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CountryLoadAllForShipping");
            db.AddInParameter(dbCommand, "ShowHidden", DbType.Boolean, showHidden);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    Country country = new Country();
                    country.CountryID = NopDataHelper.GetInt(dataReader, "CountryID");
                    country.Name = NopDataHelper.GetString(dataReader, "Name");
                    country.AllowsRegistration = NopDataHelper.GetBoolean(dataReader, "AllowsRegistration");
                    country.AllowsBilling = NopDataHelper.GetBoolean(dataReader, "AllowsBilling");
                    country.AllowsShipping = NopDataHelper.GetBoolean(dataReader, "AllowsShipping");
                    country.TwoLetterISOCode = NopDataHelper.GetString(dataReader, "TwoLetterISOCode");
                    country.ThreeLetterISOCode = NopDataHelper.GetString(dataReader, "ThreeLetterISOCode");
                    country.NumericISOCode = NopDataHelper.GetInt(dataReader, "NumericISOCode");
                    country.Published = NopDataHelper.GetBoolean(dataReader, "Published");
                    country.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                    countryCollection.Add(country);
                }
            }
            return countryCollection;
        }

        public static Country GetByCountryID(int CountryID)
        {
            Country country = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CountryLoadByPrimaryKey");
            db.AddInParameter(dbCommand, "CountryID", DbType.Int32, CountryID);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    country = new Country();
                    country.CountryID = NopDataHelper.GetInt(dataReader, "CountryID");
                    country.Name = NopDataHelper.GetString(dataReader, "Name");
                    country.AllowsRegistration = NopDataHelper.GetBoolean(dataReader, "AllowsRegistration");
                    country.AllowsBilling = NopDataHelper.GetBoolean(dataReader, "AllowsBilling");
                    country.AllowsShipping = NopDataHelper.GetBoolean(dataReader, "AllowsShipping");
                    country.TwoLetterISOCode = NopDataHelper.GetString(dataReader, "TwoLetterISOCode");
                    country.ThreeLetterISOCode = NopDataHelper.GetString(dataReader, "ThreeLetterISOCode");
                    country.NumericISOCode = NopDataHelper.GetInt(dataReader, "NumericISOCode");
                    country.Published = NopDataHelper.GetBoolean(dataReader, "Published");
                    country.DisplayOrder = NopDataHelper.GetInt(dataReader, "DisplayOrder");
                }
            }
            return country;
        }

        public static Country InsertCountry(string Name,
            bool AllowsRegistration, bool AllowsBilling, bool AllowsShipping,
            string TwoLetterISOCode, string ThreeLetterISOCode, int NumericISOCode,
            bool Published, int DisplayOrder)
        {
            Country country = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CountryInsert");
            db.AddOutParameter(dbCommand, "CountryID", DbType.Int32, 0);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "AllowsRegistration", DbType.Boolean, AllowsRegistration);
            db.AddInParameter(dbCommand, "AllowsBilling", DbType.Boolean, AllowsBilling);
            db.AddInParameter(dbCommand, "AllowsShipping", DbType.Boolean, AllowsShipping);
            db.AddInParameter(dbCommand, "TwoLetterISOCode", DbType.String, TwoLetterISOCode);
            db.AddInParameter(dbCommand, "ThreeLetterISOCode", DbType.String, ThreeLetterISOCode);
            db.AddInParameter(dbCommand, "NumericISOCode", DbType.Int32, NumericISOCode);
            db.AddInParameter(dbCommand, "Published", DbType.Boolean, Published);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                int CountryID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@CountryID"));
                country = GetByCountryID(CountryID);
            }
            return country;
        }

        public static Country UpdateCountry(int CountryID, string Name,
            bool AllowsRegistration, bool AllowsBilling, bool AllowsShipping,
            string TwoLetterISOCode, string ThreeLetterISOCode, int NumericISOCode,
            bool Published, int DisplayOrder)
        {
            Country country = null;
            Database db = NopDataHelper.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Nop_CountryUpdate");
            db.AddInParameter(dbCommand, "CountryID", DbType.Int32, CountryID);
            db.AddInParameter(dbCommand, "Name", DbType.String, Name);
            db.AddInParameter(dbCommand, "AllowsRegistration", DbType.Boolean, AllowsRegistration);
            db.AddInParameter(dbCommand, "AllowsBilling", DbType.Boolean, AllowsBilling);
            db.AddInParameter(dbCommand, "AllowsShipping", DbType.Boolean, AllowsShipping);
            db.AddInParameter(dbCommand, "TwoLetterISOCode", DbType.String, TwoLetterISOCode);
            db.AddInParameter(dbCommand, "ThreeLetterISOCode", DbType.String, ThreeLetterISOCode);
            db.AddInParameter(dbCommand, "NumericISOCode", DbType.Int32, NumericISOCode);
            db.AddInParameter(dbCommand, "Published", DbType.Boolean, Published);
            db.AddInParameter(dbCommand, "DisplayOrder", DbType.Int32, DisplayOrder);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                country = GetByCountryID(CountryID);
            return country;
        }
        #endregion
    }
}
